<%@ page language="java" isELIgnored="false" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色信息管理</title>
     <%@include file="/common/include.jsp"%>
     	<script>
		$(function(){
			$('#roleForm').hide();
			$('#roleList').datagrid({
				//title: '角色列表',
				pagination:true,
				nowrap: false,
                striped: true,
                collapsible: true,
				toolbar:[{
					text:'新增',
					iconCls:'icon-add',
					handler:function(){WinOpen();}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){modify();}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){remove();}
				},'-',{
					text:'查询',
					iconCls:'icon-search',
					handler:function(){
					    $('#roleList').datagrid('options').url = 'admin/roleAction!roleListJSON.action?isSearch=true&_query.names='+$('#names').val();
					    $('#roleList').datagrid("reload");
					    reloadTree();
					}
				}],
				 rownumbers:true,
				 frozenColumns:[[
				    {field:'ck',checkbox:true}
				]],
				onLoadSuccess: function () {
				    $('.datagrid-toolbar').append($('#searchTable'));
				    $('#searchTable').show();
				    $('#saveMenu').linkbutton({disabled:true});
					$('body').layout('collapse','east');
				    var roleListPager = $(this).datagrid('getPager');
					if (roleListPager){	
						$(roleListPager).pagination({
							onSelectPage:function(pageNumber, pageSize){
								$('#roleList').datagrid('reload');
								reloadTree();
							}
						});
					}
				}
			});
			$('#menuTree').tree({
				checkbox: true,
				url: 'admin/menuAction!menuTreeJSON.action'
			});
		});
		function WinOpen(ids){
			   $('#roleForm').form('clear');
			   $('#roleWindows').window('open');
			   $('#roleWindows').panel('setTitle','增加角色');
			   if(ids){
				   $('#roleWindows').panel('setTitle','修改角色');
				   $('#roleForm').form('load', 'admin/roleAction!viewRole.action?role.ids='+ids); 
			   }
		       $('#roleForm').show();
			}
		function modify(){
			var rows = $('#roleList').datagrid('getSelections');
			var selected = $('#roleList').datagrid('getSelected');
			if (rows.length<2&&selected){
				WinOpen(selected.ids);
		    }else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请选择一条数据！</b></div>','warning');
		    }
		}
		function remove(){
			var selectedRows = $('#roleList').datagrid('getSelections');
			var delIds = '';
			if (selectedRows.length>0){
				for(var i=0; i<selectedRows.length; i++){
					if (delIds != '') delIds += ',';
					delIds += selectedRows[i].ids;
				}
				$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>确定删除？</b></div>', function(r){
					if (r){
						var url="admin/roleAction!deleteRole.action";
						$.post(
								url,
								{"delIds":delIds},
								function(data){
									eval('data='+data);
						        	var state = data.state;
						        	var retMsg = data.resultMsg;
						        	if(state==true){
						        		$('#roleList').datagrid("reload");
						        		$('#menuForm').form('clear');
						        		reloadTree();
						        	}
						        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
								}
						);
					}
				});

			}else{
		    	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>请至少选择一条数据！</b></div>','warning');
		    }
		}

		function save(){
			//添加和修改保存
			$('#roleForm').form('submit',{
		        onSubmit: function(){     	
		        	return $('#roleForm').form('validate');
		        },
		        success:function(data){
		        	eval('data='+data);
		        	var state = data.state;
		        	var retMsg = data.resultMsg;
		        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
		        	if(state==true){
		    			$('#roleList').datagrid('reload');
						$('#roleWindows').window('close');
			        	$('#roleForm').form('clear');
			        	$('#menuForm').form('clear');
			        	reloadTree();
		        	}
		        }
			});
		}
		function formatterAct(rowIndex,rowData){
			return '<a href="#" onclick="asignMenu('+rowData.ids+',\''+rowData.names+'\')"><img alt="分配菜单..." src="${pageContext.request.contextPath }/images/kcmsystem.png" border="0"></img></a>';
		}
		function reloadTree(){
			var roelIds = $('#menuForm').attr('role.ids').value;
			if(roelIds){
	        	$('#menuForm').form('clear');
				$('#menuTree').tree('options').url="admin/menuAction!menuTreeJSON.action";
				$('#menuTree').tree('reload');
				$('body').layout('collapse','east');
			}
		}
		function asignMenu(ids,names){
			$('#roleList').datagrid('unselectAll');
			if(names.length>6)
				names = names.substr(0,5)+'...';
			$('#menuTreePanel').panel('setTitle','分配【'+names+'】角色菜单');
			var roleIds = $('#menuForm').attr('role.ids').value;
			if(!roleIds)
				$('body').layout('expand','east');
			$('#menuForm').attr('role.ids').value = ids;//角色ID
			$('#menuTree').tree('options').url="admin/menuAction!menuTreeJSON.action?role.ids="+ids;
			$('#menuTree').tree('reload');
			$('#saveMenu').linkbutton({disabled:false});
		}
		function saveMenu(){
			var nodes = $('#menuTree').tree('getChecked');
            var ids = '';
            var msg = "将删除该角色所有菜单,确定操作？";
			if(nodes){
				for(var i=0; i<nodes.length; i++){
					if (ids != '') ids += ',';
					ids += nodes[i].id;
				}
			}
		    if(ids)
		        msg="确定分配该角色的菜单？";
			$.messager.confirm('提示信息', '<div style="padding-top:15px;padding-left:5px"><b>'+msg+'</b></div>', function(r){
				if (r){
					$('#menuForm').attr('menuIds').value = ids;
					$('#menuForm').form('submit',{
				        success:function(data){
				        	eval('data='+data);
				        	var state = data.state;
				        	var retMsg = data.resultMsg;
				        	$.messager.alert('提示信息','<div style="padding-top:15px;padding-left:5px"><b>'+retMsg+'</b></div>','info');
				        	if(state==true){
				        		var roelIds = $('#menuForm').attr('role.ids').value;//角色ID
				    			$('#menuTree').tree('options').url="admin/menuAction!menuTreeJSON.action?role.ids="+roelIds;
				        		$('#menuTree').tree('reload');
					        	//$('#menuForm').form('clear');
				        	}
				        }
					});
				}
			});
		}
	</script>
</head>
<body class="easyui-layout">
  <div id="roleWindows" class="easyui-window" closed="true" iconCls="icon-save" modal="true" resizable="false" maximizable
  ="false" minimizable
  ="false" title="添加角色" style="width:450px;height:208px;padding:5px;background:#fafafa;">
	 <div region="center" border="false" style="background:#fff; border:1px solid #ccc;">
	 	<%@include file="roleInfo.jsp"%>
     </div>
     <div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
     	<a class="easyui-linkbutton" icon="icon-save" href="javascript:void(0)"  onclick="save()">保存</a>
     	<a class="easyui-linkbutton" icon="icon-back" href="javascript:void(0);" onclick="javascript:$('#roleWindows').window('close');">返回</a>
     </div>
  </div>
  <div region="center"  title="角色列表"  style="padding:5px;" border="false">
	<table id="roleList" fit="true" url="admin/roleAction!roleListJSON.action">
		<thead>
			<tr>
				<th field="names" width="200">角色名</th>
				<th field="description" width="300">角色描述</th>
				<th field="ids" width="30" formatter="formatterAct">操作</th>
			</tr>
		</thead>
	</table>	
</div>
 <div id="menuTreePanel" region="east" title="菜单树" border=false split="true" style="width:230px;padding:5px;">
	<form id="menuForm" method="post" action="admin/roleAction!roleMenuSave.action">
    	<input type="hidden" name="role.ids">
    	<input type="hidden" name="menuIds">
		<ul id="menuTree"></ul>
	</form>
 </div>
<table id="searchTable" style="display:none" border="0" cellpadding="0">
	<tr>
		<td width="20%">
			<input type="text" id="names"/>
		</td>
		<td  width="20%">
			<B>角色名称</B>
		</td>
		 <td  width="60%" align="right">
			<a href="#" onclick="saveMenu()" class="easyui-linkbutton" id="saveMenu" plain="true" iconCls="icon-save" style="disabled">保存分配菜单</a>
		</td>
	<tr>
</table>
</body>
</html>